﻿using System;
using System.Collections.Generic;
using System.Text;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Persist.DAL.Dao;
using Telegnosis.Framework.Constants;
using Telegnosis.Framework.DataAccess.DB;

namespace CharacterTrailLibrary.Impl
{
    public class ProcedurePersistImpl:IProcedurePersist
    {
        public List<Persist.BLL.Model.TProcedureModel> GetProcedureModels()
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text =  " 9=9 ";
            var dao = new TProcedureDao();
            return dao.GetList(sqlWhere);
        }

        public Persist.BLL.Model.TProcedureModel GetProcedureModel(string procedureNo)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text =String.Format( " ProcedureNo='{0}'",procedureNo);
            var dao = new TProcedureDao();
            var list =  dao.GetList(sqlWhere);
            if (list.Count > 0)
                return list[0];
            return null;
        }

        public void SaveProcedureModels(List<Persist.BLL.Model.TProcedureModel> list)
        {
            ITrans trans = DAHelper.GetSqlTrans();
            var dao = new TProcedureDao { Trans = trans };
            try
            {
                trans.Begin();
                foreach (var model in list)
                {
                    if (model.EditFlag == Enums.EditFlag.New)
                    {
                        dao.InsertItem(model.CreateOrm());
                        model.EditFlag = Enums.EditFlag.None;
                    }
                    else if (model.EditFlag == Enums.EditFlag.Update)
                    {
                        dao.UpdateItem(model.CreateOrm());
                        model.EditFlag = Enums.EditFlag.None;
                    }
                }
                trans.Commit();
            }
            catch (Exception exception)
            {
                trans.RollBack();
                throw;
            }
        }

        //create table TProcedure
        //(
        //    ProcedureNo  varchar(10) ,--工序号
        //    ProcedureName  varchar(50) ,--工序名称
        //    PStatus int default 0,--状态
        //    ParentNumber  varchar(10),--父工序号代码
        //)

        public void UpdateProcedureModel(string procedureNoOld ,string procedureNoNew, string procedureNameNew)
        {
            if (String.IsNullOrEmpty(procedureNoNew) ) throw new Exception("工序代码为空！");
            if (String.IsNullOrEmpty(procedureNameNew )) throw new Exception("工序名称为空！");

            ISqlAccess sqlAccess = DAHelper.GetSqlAccess();
            string sqlUpdate1 = "update TProcedure set ProcedureNo = '{0}',ProcedureName= '{1}' where ProcedureNo='{2}'";
            string sqlUpdate2 = "update TProcedure set ParentNumber = '{0}' where ParentNumber='{1}'";

            sqlUpdate1 = String.Format(sqlUpdate1, procedureNoNew, procedureNameNew, procedureNoOld);
            sqlUpdate2 = String.Format(sqlUpdate2, procedureNoNew,  procedureNoOld);

            try
            {
                //如果工序代码改变，就要检查新的工序代码是否被占用。
                if (procedureNoOld != procedureNoNew)
                {
                    var model = GetProcedureModel(procedureNoNew);
                    if (model != null)
                    {
                        throw new Exception(String.Format("工序代码{0}已经被占用！", procedureNoNew));
                    }
                }
                sqlAccess.ExecuteTransaction(new SqlItem[] {new SqlItem(sqlUpdate1), new SqlItem(sqlUpdate2),});
            }
            catch (Exception exception)
            {
                throw new Exception("记录修改错误：" + exception.Message);
            }
        }


        public void NewProcedureModel(string parentProcedureNo, string procedureNo, string procedureName)
        {
            var modelTmp = GetProcedureModel(procedureNo);
            if (modelTmp != null)
            {
                throw new Exception(String.Format("工序代码{0}已经被占用！", procedureNo));
            }

            var dao = new TProcedureDao();
            try
            {
                var model = new TProcedureModel
                                {
                                    ProcedureNo = procedureNo,
                                    ProcedureName = procedureName,
                                    PStatus = 0,
                                    ParentNumber = parentProcedureNo
                                };

                dao.InsertItem(model.CreateOrm());
            }
            catch (Exception exception)
            {
                throw new Exception("记录插入错误：" + exception.Message);
            }
        }


        public void DeleteChildProcedure(string procedureNo)
        {
            var list = GetChildModels(procedureNo);
            if(list.Count>0)
            {
                throw  new Exception(String.Format("工序{0}存在子工序，不能删除！",procedureNo));
            }
            var dao = new TProcedureDao();
            var model = GetProcedureModel(procedureNo);
            if(model == null)
            {
                throw new Exception(String.Format("工序{0}不存在，不能删除！", procedureNo));
            }
            dao.DeleteItem(model.CreateOrm());
        }


        public List<TProcedureModel> GetChildModels(string parentProcedureNo)
        {
            var sqlWhere = new SqlItem();
            sqlWhere.Text = String.Format(" ParentNumber='{0}' ", parentProcedureNo);
            var dao = new TProcedureDao();
            return dao.GetList(sqlWhere);
        }
    }
}
